﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gasco.DataAccess;
using Gasco.Business.Services;

public partial class Controls_lookup_index : System.Web.UI.UserControl
{


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
         LoadLookup(null,1,true,gvlookup.SortExpression,gvlookup.SortDirection.ToString());
        }
    }

    private void LoadLookup(string keyword,int pageNumber,bool setPageCount,string sortBy,string desc)
    {
        ViewState["CurrentPage"] = pageNumber;
        string typeName = Request.QueryString["type"];

        if (String.IsNullOrEmpty(typeName))
            return;


        switch (typeName.ToLower())
        {
            case "country":
                LookupService<country> countryService = new LookupService<country>();
                if (setPageCount) gvlookup.VirtualItemCount = countryService.CountSearchAll(keyword);
                gvlookup.DataSource = countryService.SearchAll(tbSearch.Text, gvlookup.PageSize, pageNumber, sortBy, desc);
                break;
            case "contact_type":
                LookupService<contact_type> contactTypeService = new LookupService<contact_type>();
                if (setPageCount) gvlookup.VirtualItemCount = contactTypeService.CountSearchAll(keyword);
                gvlookup.DataSource = contactTypeService.SearchAll(tbSearch.Text, gvlookup.PageSize, pageNumber, sortBy, desc);
                break;
            case "order_type":
                LookupService<order_type> orderTypeService = new LookupService<order_type>();
                if (setPageCount) gvlookup.VirtualItemCount = orderTypeService.CountSearchAll(keyword);
                gvlookup.DataSource = orderTypeService.SearchAll(tbSearch.Text, gvlookup.PageSize, pageNumber, sortBy, desc);
                break;
            case "platts_type":
                LookupService<platts_type> plattsTypeService = new LookupService<platts_type>();
                if (setPageCount) gvlookup.VirtualItemCount = plattsTypeService.CountSearchAll(keyword);
                gvlookup.DataSource = plattsTypeService.SearchAll(tbSearch.Text, gvlookup.PageSize, pageNumber, sortBy, desc);
                break;
            case "product_type":
                LookupService<product_type> productTypeService = new LookupService<product_type>();
                if (setPageCount) gvlookup.VirtualItemCount = productTypeService.CountSearchAll(keyword);
                gvlookup.DataSource = productTypeService.SearchAll(tbSearch.Text, gvlookup.PageSize, pageNumber, sortBy, desc);
                break;
            case "unit":
                LookupService<unit> unitService = new LookupService<unit>();
                if (setPageCount) gvlookup.VirtualItemCount = unitService.CountSearchAll(keyword);
                gvlookup.DataSource = unitService.SearchAll(tbSearch.Text, gvlookup.PageSize, pageNumber, sortBy, desc);
                break;
            case "order_status":
                LookupService<order_status> orderStatus = new LookupService<order_status>();
                if (setPageCount) gvlookup.VirtualItemCount = orderStatus.CountSearchAll(keyword);
                gvlookup.DataSource = orderStatus.SearchAll(tbSearch.Text, gvlookup.PageSize, pageNumber, sortBy, desc);
                break;
            case "currency":
                LookupService<currency> currencyService = new LookupService<currency>();
                if (setPageCount) gvlookup.VirtualItemCount = currencyService.CountSearchAll(keyword);
                gvlookup.DataSource = currencyService.SearchAll(tbSearch.Text,gvlookup.PageSize,pageNumber,sortBy,desc);
                break;
            case "transportation_type":
                LookupService<transportation_type> transportService=new LookupService<transportation_type>();
                if (setPageCount) gvlookup.VirtualItemCount = transportService.CountSearchAll(keyword);
                gvlookup.DataSource = transportService.SearchAll(tbSearch.Text, gvlookup.PageSize, pageNumber, sortBy, desc);
                break;
            case "border":
                LookupService<border> borderService = new LookupService<border>();
                if (setPageCount) gvlookup.VirtualItemCount = borderService.CountSearchAll(keyword);
                gvlookup.DataSource = borderService.SearchAll(tbSearch.Text, gvlookup.PageSize, pageNumber, sortBy, desc);
                break;
        }

        gvlookup.DataBind();
    }

    protected void gvlookup_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvlookup.PageIndex = e.NewPageIndex;
        LoadLookup(tbSearch.Text,e.NewPageIndex + 1, false,gvlookup.SortExpression,gvlookup.SortDirection.ToString());
    }

    protected void gvlookup_Deleting(object sender, GridViewDeleteEventArgs e)
    {
        string typeName = Request.QueryString["type"];
        object key = gvlookup.DataKeys[e.RowIndex].Value;

        if (String.IsNullOrEmpty(typeName))
            return;

        switch (typeName.ToLower())
        {
            case "country":
                LookupService<country> countryService = new LookupService<country>();
                countryService.Delete(Convert.ToInt32(key));
                break;
            case "contact_type":
                LookupService<contact_type> contactTypeService = new LookupService<contact_type>();
                contactTypeService.Delete(Convert.ToInt32(key));
                break;
            case "order_type":
                LookupService<order_type> orderTypeService = new LookupService<order_type>();
                orderTypeService.Delete(Convert.ToInt32(key));
                break;
            case "platts_type":
                LookupService<platts_type> plattsTypeService = new LookupService<platts_type>();
                plattsTypeService.Delete(Convert.ToInt32(key));
                break;
            case "product_type":
                LookupService<product_type> productTypeService = new LookupService<product_type>();
                productTypeService.Delete(Convert.ToInt32(key));
                break;
            case "unit":
                LookupService<unit> unitService = new LookupService<unit>();
                unitService.Delete(Convert.ToInt32(key));
                break;
            case "order_status":
                LookupService<order_status> orderStatus = new LookupService<order_status>();
                orderStatus.Delete(Convert.ToInt32(key));
                break;
            case "currency":
                LookupService<currency> currencyService = new LookupService<currency>();
                currencyService.Delete(Convert.ToInt32(key));
                break;
            case "transportation_type":
                LookupService<transportation_type> transportService = new LookupService<transportation_type>();
                transportService.Delete(Convert.ToInt32(key));
                break;
            case "border":
                LookupService<border> borderService = new LookupService<border>();
                borderService.Delete(Convert.ToInt32(key));                
                break;
        }

        LoadLookup(tbSearch.Text, 1, true, gvlookup.SortExpression, gvlookup.SortDirection.ToString());
    }
    protected void gvlookup_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["sort"] == null || ViewState["sort"].ToString() == "asc")
        {
            e.SortDirection = SortDirection.Descending;
            ViewState["sort"] = "desc";
        }
        else 
        {
            e.SortDirection = SortDirection.Ascending;
            ViewState["sort"] = "asc";
        }
        LoadLookup(tbSearch.Text, gvlookup.PageIndex + 1, false, e.SortExpression, e.SortDirection.ToString());
    }

    protected void lbSearch_Click(object sender, EventArgs e)
    {
        LoadLookup(tbSearch.Text, 1, true, gvlookup.SortExpression, gvlookup.SortDirection.ToString());
    }
}
